package com.spinach.game.config;

import com.spinach.game.core.GamePlayer;
import com.spinach.game.core.Room;
import com.spinach.game.core.UserInfo;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;

@Data
@ConfigurationProperties(prefix = "netty.doudizhu")
@ConditionalOnProperty(prefix = "netty.doudizhu", name = "load", havingValue = "true")
@Component
@ToString
public class DouDiZhuConfig {
    private Integer port;

    private String path;

    private Integer roomNumber;

    /**
     * 房间信息池
     */
    private final Map<Long, Room> ROOM_MAP = new ConcurrentSkipListMap<>();

    /**
     * 玩家邻接表
     */
    private final static Map<Long, UserInfo> CLIENT_PLAYER_MAP = new ConcurrentSkipListMap<>();


    public Room getRoom(Long id) {
        return ROOM_MAP.get(id);
    }

    public Map<Long, Room> getRoomMap() {
        return ROOM_MAP;
    }

    public Room removeRoom(Long id) {
        return ROOM_MAP.remove(id);
    }

    public void addRoom(Room room) {
        ROOM_MAP.put(room.getId(), room);
    }

    public UserInfo getUser(long id) {
        return CLIENT_PLAYER_MAP.get(id);
    }

    static {
        UserInfo user1 = UserInfo.builder().id(1L).nickname("user1").build();
        UserInfo user2 = UserInfo.builder().id(2L).nickname("user2").build();
        UserInfo user3 = UserInfo.builder().id(3L).nickname("user3").build();

        CLIENT_PLAYER_MAP.put(1L, user1);
        CLIENT_PLAYER_MAP.put(2L, user2);
        CLIENT_PLAYER_MAP.put(3L, user3);
    }
}
